#pragma once

#include <QtWidgets/QWidget>
#include "ui_camera.h"

#include "distance_config.h"
#include "camera_config.h"
#include "cydevice.h"
#include "image_process.h"
#include "calibration.h"
#include "calcdistance.h"
#include "pose_correct.h"
#include "auto_focus.h"

class Camera : public QWidget
{
    Q_OBJECT

public:
    Camera(QWidget *parent = Q_NULLPTR);
    ~Camera();

    void openCamera();
    void closeCamera();
    void receive();

    void changeResolution(int index);

signals:
    void setResolution(int width);

public slots:
    void calibrationFinished();
    void calcDistanceFinished(double length);

protected:
    void closeEvent(QCloseEvent* event) override;

private:
    void initWindows();
    void initConfig();
    void initCamera();
    void initProcess();
    void initCalibration();
    void initCalcDistance();
    void initAutoFocus();

    void imshow(cv::Mat& image);

    Ui::cameraClass ui;
    CameraConfig::Config cameraConfig;
    CameraConfig::Command cameraCommand;
    DistanceConfig::CalibrationConfig calibrationConfig;
    DistanceConfig::DistanceConfig distanceConfig;

    QThread cameraThread;
    QThread processThread;
    QThread focusThread;

    bool isOpen;
    bool isColor;

    bool isCapture;
    int capturedCount;
    QList<cv::Mat> calibImages;
    Calibration calibration;
    bool isCorrect;

    bool isCalcDist;
    bool isCross;
    CalcDistance calcDistance;
    PoseCorrect* poseCorrect;

    AutoFocus autoFocus;

    QTimer timer;
    int frames;
	
    QMap<int, Distance*> distanceMethods;
};
